// pages/postcard/postcard.js
const {
  GetCard,
  GetOtherCard,
  CardChange,
  CardImage
} = require('../../utils/api')
import confg from '../../utils/config';
import util from '../../utils/util';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    confg: confg,
    is_card: 0,
    card_data: {
      name: 'xxx',
      company: 'xxx',
      position: 'xxx',
      mobile: '-',
      wechat: '-',
      province: '-'
    },
    card_type: 0,
    shareobj: '',
    type2ids: [],
    visible: false,
    drowImgInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.cid && options.ctype) {
      this.setData({
        shareobj: {
          type: options.ctype,
          id: options.cid
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  goAuth(e) {
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/authstore/index?type=' + type,
    })
  },
  getCardFn() {
    if (this.data.shareobj) {
      GetOtherCard(this.data.shareobj).then(res => {
        this.getCardResFn(res)
      })
    } else {
      GetCard().then(res => {
        this.getCardResFn(res)
      })
    }
  },
  getCardResFn(res) {
    if (res.code == 200) {
      let das = res.card_type == 1 ? res.data : res.data[0] || {}
      this.setData({
        is_card: 2,
        card_type: res.card_type,
        card_data: das,
        card_data_all: res.data
      })
      if (res.card_type == 2) {
        let arr = [];
        res.data.forEach(el => {
          arr.push(el.platform)
        })
        this.setData({
          type2ids: arr.join(',')
        })
      }
    } else if(res.code==4005){
      this.setData({
        is_card: 1
      })
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '您还没有登录，请登录',
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }
        }
      })
    }else{
      this.setData({
        is_card: 1
      })
    }
  },
  cutCurCard(e) {
    let data = this.data.card_data_all;
    let _this = this;
    if (data.length == 1) {
      wx.showModal({
        title: '提示',
        content: '您只有一张明片，请添加!',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/authstore/index?type=2&ids=' + _this.data.type2ids
            })
          }
        }
      })
    } else {
      let nextid = 0;
      let arr = [];
      data.forEach(el => {
        if (el.id != e.currentTarget.dataset.id) {
          arr.push(el.id)
        }
      })
      nextid = arr[Math.floor(Math.random() * arr.length)]
      CardChange({
        id: nextid
      }).then(res => {
        this.getCardFn()
      })
    }
  },
  goHome(e) {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCardFn()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const data = this.data.card_data;
    const shares = {
      title: this.data.card_type == 1 ? data.name : data.nature_name,
      path: `/pages/postcard/postcard?ctype=${this.data.card_type}&cid=${data.id}`
    }
    return shares
  },
  addcardfn() {
    wx.navigateTo({
      url: '/pages/authstore/index?type=2&ids=' + this.data.type2ids
    })
  },
  chareCodeFn() {
    let ob = {
      cid: this.data.card_data.id,
      ctype: this.data.card_type
    };
    CardImage(ob).then(res => {
      this.setData({
        drowImgInfo:{
          wximg:confg.base_url + res.data,
          card:this.data.card_data,
          type:this.data.card_type
        }
      })
      this.show()
    })
  },
  show: function () {
    this.setData({
      visible: true
    })
  },
  close: function () {
    this.setData({
      visible: false
    })
  },
})